package app.service.goods;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import app.model.goods.GoodsStock;
import app.util.StringUtil;
import base.PageInfo;
import base.category.sys.YesOrNo;
import base.dao.HibernateDao;
import base.service.BaseService;

@Service
public class GoodsStockService extends BaseService {

	@Autowired
	private HibernateDao<GoodsStock> hibernateDao;
	
	@Override
	public HibernateDao<GoodsStock> getDao() {
		return this.hibernateDao;
	}
	
	/**
	 * 查询商品
	 * @param pageInfo
	 * @param goodsName 
	 * @return
	 */
	public PageInfo list(PageInfo pageInfo, String goodsName) {
		String sql = "SELECT gb.*,IFNULL(gsr.stock_total_price,0) stock_total_price,IFNULL(gsr.stock_goods_num,0) stock_goods_num,IFNULL(gdr.delivery_total_price,0) delivery_total_price,IFNULL(gdr.delivery_goods_num,0) delivery_goods_num,"
				+ "(IFNULL(gdr.delivery_total_price,0)-IFNULL(gsr.stock_total_price,0)) net_income,(IFNULL(gsr.stock_goods_num,0)-IFNULL(gdr.delivery_goods_num,0)) stock_surplus "
				+ "FROM goods_base gb LEFT JOIN (SELECT SUM(a.goods_price*a.goods_num) stock_total_price,SUM(a.goods_num) stock_goods_num,a.goods_id FROM goods_stock_records a GROUP BY a.goods_id) gsr " + " ON gb.id=gsr.goods_id LEFT JOIN "
				+ "(SELECT SUM(a.goods_price*a.goods_num) delivery_total_price,SUM(a.goods_num) delivery_goods_num,a.goods_id FROM goods_delivery_records a GROUP BY a.goods_id) gdr " + "ON gb.id=gdr.goods_id where gb.is_use = " + YesOrNo.YES_INT;
		if(StringUtil.isNotEmpty(goodsName)){
			sql += " and gb.goods_name like '%"+goodsName+"%'";
		}
		sql += " order by gb.create_time desc";
		PageInfo page = getDao().queryPageListBySql(pageInfo, sql);
		return page;
	}

	public void saveEntity(GoodsStock entity) {
		getDao().save(entity);
	}

	public void updateEntity(GoodsStock entity) {
		getDao().mergeAndUpdate(entity);
	}

	public GoodsStock queryEntityById(String id) {
		String sql = "select * from goods_stock a where a.id = '"+id+"'";
		Object entity = getDao().queryEntityBySql(sql , GoodsStock.class);
		return (GoodsStock)entity;
	}

	public void delEntity(String ids) {
		if(!StringUtils.isEmpty(ids)){
			String[] idsArray = ids.split(",");
			for (int i = 0; i < idsArray.length; i++) {
				GoodsStock gb = new GoodsStock();
				gb.setId(idsArray[i]);
				getDao().del(gb);
			}
		}
	}
	
//	/**
//	 * 
//	 * @param goodsId
//	 * @param goodsStock
//	 * @param opt "add" or "sub"
//	 */
//	public void changeGoodsStock(String goodsId,Double goodsStock,String opt){
//		String hql = "select o from GoodsStock o where o.id = '"+goodsId+"'";
//		GoodsStock entity = getDao().queryEntityByHQL(hql);
//		Double oldGoodsStock = entity.getGoodsStock();
//		if("add".equals(opt)){
//			entity.setGoodsStock(oldGoodsStock+goodsStock);
//		}
//		if("sub".equals(opt)){
//			entity.setGoodsStock(oldGoodsStock-goodsStock);
//		}
//		getDao().mergeAndUpdate(entity);
//	}
}
